package cn.libinal.scloud.base.common.exception;

import cn.libinal.scloud.base.common.web.ResultJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author libinal
 */
@Slf4j
@RestControllerAdvice
public class GlobalControllerExceptionHandler {

    /**
     * 把值绑定到Model中，使全局@RequestMapping可以获取到该值
     *
     * @param model Model
     */
    @ModelAttribute
    public void addAttributes(Model model) {
        model.addAttribute("author", "libinal");
    }

    /**
     * 500
     *
     * @param e 异常信息
     * @return 异常信息
     */
    @ExceptionHandler(value = Exception.class)
    public ResultJson<String> defaultErrorHandler(Exception e) {
        log.error("Exception", e);
        return ResultJson.error(e.getMessage());
    }
}
